﻿using System;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Data;
using YiSha.Data.Repository;
using YiSha.Entity.QixingManage;
using YiSha.Model.Param.QixingManage;
using YiSha.Enum.QixingManage;

namespace YiSha.Service.QixingManage
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2023-04-09 19:28
    /// 描 述：优惠劵服务类
    /// </summary>
    public class CouponService :  RepositoryFactory
    {
        #region 获取数据
        public async Task<List<CouponEntity>> GetList(CouponListParam param)
        {
            var expression = ListFilter(param);
            var list = await this.BaseRepository().FindList(expression);
            return list.ToList();
        }

        public async Task<List<CouponEntity>> GetList(Expression<Func<CouponEntity, bool>> expression)
        {
            var list = await this.BaseRepository().FindList(expression);
            return list.ToList();
        }

        public async Task<List<CouponEntity>> GetPageList(CouponListParam param, Pagination pagination)
        {
            var expression = ListFilter(param);
            var list= await this.BaseRepository().FindList(expression, pagination);

            foreach (var item in list)
            {
                if (item.end_time<DateTime.Now)
                {
                    item.status = ((int)CouponStatus.已过期);
                }
                else if (item.start_time > DateTime.Now)
                {
                    item.status = ((int)CouponStatus.未使用);
                }
                else
                {
                    item.status = ((int)CouponStatus.使用中);
                }
            }

            return list.ToList();
        }

        public async Task<CouponEntity> GetEntity(long id)
        {
            return await this.BaseRepository().FindEntity<CouponEntity>(id);
        }
        #endregion

        #region 提交数据
        public async Task SaveForm(CouponEntity entity)
        {
            if (entity.Id.IsNullOrZero())
            {
                entity.Create();
                await this.BaseRepository().Insert(entity);
            }
            else
            {
                
                await this.BaseRepository().Update(entity);
            }
        }

        public async Task DeleteForm(string ids)
        {
            long[] idArr = TextHelper.SplitToArray<long>(ids, ',');
            await this.BaseRepository().Delete<CouponEntity>(idArr);
        }

        public async Task DeleteForm(Expression<Func<CouponEntity, bool>> expression)
        {
            await this.BaseRepository().Delete<CouponEntity>(expression);
        }
        
        #endregion

        #region 私有方法
        private Expression<Func<CouponEntity, bool>> ListFilter(CouponListParam param)
        {
            var expression = LinqExtensions.True<CouponEntity>();
            if (param != null)
            {
                if (!param.title.IsEmpty()) 
                {
                    expression = expression.And(t => t.title.Contains(param.title));
                }
                if (!param.tenant_id.IsNullOrZero())
                {
                    expression = expression.And(t => t.tenant_id == param.tenant_id);
                }
            }
            return expression;
        }
        #endregion
    }
}
